package sg.edu.nus.iss.ooad.jass.ui;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import sg.edu.nus.iss.ooad.jass.controller.CompanyController;
import sg.edu.nus.iss.ooad.jass.controller.JobseekerController;
import sg.edu.nus.iss.ooad.jass.controller.MainController;
import sg.edu.nus.iss.ooad.jass.entity.Company;
import sg.edu.nus.iss.ooad.jass.entity.JobAdvertisement;

import sg.edu.nus.iss.ooad.jass.entity.Jobseeker;
import sg.edu.nus.iss.ooad.jass.entity.LoginInfo;
import sg.edu.nus.iss.ooad.jass.entity.Response;
import sg.edu.nus.iss.ooad.jass.ui.widget.AbstractJASSRegularWindow;
import sg.edu.nus.iss.ooad.jass.ui.widget.JASSRegularPanel;

/**
 *
 *  Job Advertisement Detail Form.
 *
 * @author Isak Rabin (HT082193M)
 * @version 1.0
 * @since 1.0
 *
 *
 */
public class JASSAdvertisementDetailForm extends AbstractJASSRegularWindow {

        private static final long serialVersionUID = -2975380343707801964L;
        private static final Logger logger = Logger.getLogger(JASSAdvertisementDetailForm.class.getName());
        //Form components
        private Label labelErrorMessage = null;
        private Label labelCompanyID = null;
        private Label labelStartDate = null;
        private Label labelFinishDate = null;
        private Label labelJobTitle = null;
        private Label labelJobDescription = null;
        private Label labelJobLocation = null;
        private Label labelJobStartDate = null;
        private Label labelCompanyDepartment = null;
        private Label labelTechnicalSkills = null;
        private Label labelManagementSkills = null;
        private Label labelMinSalary = null;
        private Label labelMaxSalary = null;
        private Label textCompanyID = null;
        private Label textStartDate = null;
        private Label textFinishDate = null;
        private Label textJobTitle = null;
        private Label textJobDescription = null;
        private Label textJobLocation = null;
        private Label textJobStartDate = null;
        private Label textCompanyDepartment = null;
        private Label textTechnicalSkills = null;
        private Label textManagementSkills = null;
        private Label textMinSalary = null;
        private Label textMaxSalary = null;
        private Label textJobAdvertisementId = null;
        private Button btnApplyJob = null;
        private Button btnEditProfile = null;
        private Button btnCancel = null;
        private Button btnRegister = null;
        private JASSRegularPanel formPanel = null;
        private Panel inputPanel = null;
        private Panel buttonPanel = null;
        private GridBagLayout inputLayout = null;
        private Panel notePanel = null;
        private Label LabelNoteMsg = null;
        private JobAdvertisement jobadvertisement = null;

       /**
        * The contructor of JASSAdvertisementDetailForm class
        */
        public JASSAdvertisementDetailForm() {
                super("Job Advertisement and Search System", new Dimension(800, 600));
                initComponents();

        }

        /**
         * Get JobAdvertisement
         * @return
         */
        public JobAdvertisement getJobadvertisement() {
                return jobadvertisement;
        }

        /**
         * Set JobAdvertisement
         * @param jobadvertisement
         */
        public void setJobadvertisement(JobAdvertisement jobadvertisement) {
                this.jobadvertisement = jobadvertisement;
        }

        /**
         * Get comapny details.
         * @param companyID
         */
        public void getcompany (String companyID)
        {
             try{
                CompanyController companyController = new CompanyController();
                Company company = companyController.retrieveCompany(companyID );
                if (company.isVisible())
                textCompanyID.setText(company.getName());
                }catch(Exception e)
                {
                    logger.log(Level.SEVERE, null, e);
                }
        }

        /**
         * Attach the component to the form
         * @param c
         * @param gridx
         * @param gridy
         * @param gridwidth
         * @param gridheight
         * @param fill
         */

        @Override
        public void attachComponent(Component c, int gridx, int gridy, int gridwidth, int gridheight, int fill) {
                GridBagConstraints constraint = new GridBagConstraints();
                constraint.gridx = gridx;
                constraint.gridy = gridy;
                constraint.gridwidth = gridwidth;
                constraint.gridheight = gridheight;
                constraint.fill = fill;
                constraint.insets = new Insets(4, 4, 4, 4);
                constraint.weightx = 1.0;
                constraint.weighty = 1.0;
                inputPanel.add(c);
                inputLayout.setConstraints(c, constraint);
        }

        /**
         * Display the field value
         */
        public void display() {
                getcompany (getJobadvertisement().getCompanyID());
                textJobTitle.setText(getJobadvertisement().getJobTitle());
                textJobLocation.setText(getJobadvertisement().getJobLocation());
                textJobDescription.setText(getJobadvertisement().getJobDescription());
                textCompanyDepartment.setText(getJobadvertisement().getCompanyDepartment());
                textTechnicalSkills.setText(getJobadvertisement().getTechnicalSkills());
                textManagementSkills.setText(getJobadvertisement().getManagementSkills());
                textMaxSalary.setText(String.valueOf(getJobadvertisement().getMaxSalary()));
                textMinSalary.setText(String.valueOf(getJobadvertisement().getMinSalary()));
                textStartDate.setText(getJobadvertisement().getJobStartDate());
                textFinishDate.setText(getJobadvertisement().getFinishDate());
                textJobAdvertisementId.setText(getJobadvertisement().getID());
                textJobStartDate.setText(getJobadvertisement().getJobStartDate());
        }

        /**
         * Initialize Form Componentss
         */
        private void initComponents() {


                labelCompanyID = new Label("Company ID", Label.LEFT);
                labelStartDate = new Label("Ads. Start Date (dd/MMM/yyyy)", Label.LEFT);
                labelFinishDate = new Label("Ads. Finish Date (dd/MMM/yyyy)", Label.LEFT);
                labelJobTitle = new Label("Job Title", Label.LEFT);
                labelJobDescription = new Label("Job Description", Label.LEFT);
                labelJobLocation = new Label("Job Location", Label.LEFT);
                labelJobStartDate = new Label("Job Start Date (dd/MMM/yyyy)", Label.LEFT);
                labelCompanyDepartment = new Label("Company Department", Label.LEFT);
                labelTechnicalSkills = new Label("Technical Skills", Label.LEFT);
                labelManagementSkills = new Label("Management Skills", Label.LEFT);
                labelMinSalary = new Label("Min. Salary", Label.LEFT);
                labelMaxSalary = new Label("Max. Salary", Label.LEFT);
                textCompanyID = new Label();
                textCompanyID.setSize(5, 1);
                textStartDate = new Label();
                textStartDate.setSize(15, 1);
                textFinishDate = new Label();
                textFinishDate.setSize(15, 1);
                textJobTitle = new Label();
                textJobTitle.setSize(35, 1);
                textJobDescription = new Label();
                textJobDescription.setSize(35, 5);
                textJobLocation = new Label();
                textJobLocation.setSize(35, 1);
                textJobStartDate = new Label();
                textJobStartDate.setSize(15, 1);
                textCompanyDepartment = new Label();
                textCompanyDepartment.setSize(35, 1);
                textTechnicalSkills = new Label();
                textTechnicalSkills.setSize(25, 5);
                textManagementSkills = new Label();
                textManagementSkills.setSize(25, 1);
                textMinSalary = new Label();
                textMinSalary.setSize(15, 1);
                textMaxSalary = new Label();
                textMaxSalary.setSize(15, 1);
                textJobAdvertisementId = new Label();
                btnCancel = new Button("Cancel");
                btnApplyJob = new Button("Apply");
                btnEditProfile = new Button("Edit Profile");
                btnRegister = new Button("Register");
                buttonPanel = new Panel();
                inputPanel = new Panel();

                formPanel = new JASSRegularPanel("JASS Advertisement Detail Form ");

                //set the action listener
                btnCancel.addActionListener(new CancelActionListener());
                btnApplyJob.addActionListener(new ApplyActionListener());

                //set Layout
                buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
                inputLayout = new GridBagLayout();
                inputPanel.setLayout(inputLayout);

                buttonPanel.add(btnApplyJob);
                buttonPanel.add(btnCancel);

                attachComponent(labelCompanyID, 0, 1, 1, 1);
                attachComponent(textCompanyID, 1, 1, 1, 1);
                attachComponent(labelStartDate, 6, 1, 1, 1);
                attachComponent(textStartDate, 7, 1, 2, 1);
                attachComponent(labelFinishDate, 6, 2, 1, 1);
                attachComponent(textFinishDate, 7, 2, 2, 1);
                attachComponent(labelJobTitle, 0, 3, 1, 1);
                attachComponent(textJobTitle, 1, 3, 8, 1);
                attachComponent(labelJobDescription, 0, 4, 1, 1);
                attachComponent(textJobDescription, 1, 4, 8, 4);
                attachComponent(labelJobLocation, 0, 8, 1, 1);
                attachComponent(textJobLocation, 1, 8, 6, 1);

                attachComponent(labelJobStartDate, 0, 9, 1, 1);
                attachComponent(textJobStartDate, 1, 9, 5, 1);

                attachComponent(labelCompanyDepartment, 0, 9, 1, 1);
                attachComponent(textCompanyDepartment, 1, 9, 7, 1);
                attachComponent(labelTechnicalSkills, 0, 10, 1, 1);
                attachComponent(textTechnicalSkills, 1, 10, 8, 4);
                attachComponent(labelManagementSkills, 0, 15, 1, 1);
                attachComponent(textManagementSkills, 1, 15, 8, 4);
                attachComponent(labelMinSalary, 0, 19, 1, 1);
                attachComponent(textMinSalary, 1, 19, 1, 1);
                attachComponent(labelMaxSalary, 2, 19, 1, 1);
                attachComponent(textMaxSalary, 3, 19, 1, 1);

                notePanel = new Panel();
                LabelNoteMsg = new Label("Press 'Apply' to reponse the job", Label.CENTER);
                LabelNoteMsg.setForeground(Color.RED);
                notePanel.setLayout(new BorderLayout(5, 5));
                notePanel.add(buttonPanel, BorderLayout.NORTH);
                notePanel.add(LabelNoteMsg, BorderLayout.SOUTH);


                formPanel.add(inputPanel, BorderLayout.CENTER);
                formPanel.add(notePanel, BorderLayout.SOUTH);

                //add to the frame
                add(formPanel);
                pack();



        }

        /**
         * Reset the form
         */
        @Override
        public void resetForm() {

        }

        /**
         * Cancel Action Handler. It is called when the user click  button "Cancel":
         */
        class CancelActionListener implements ActionListener {

                public void actionPerformed(ActionEvent e) {
                        close();
                }
        }

        /**
         * perform apply job Action
         */
        class ApplyActionListener implements ActionListener {

                public void actionPerformed(ActionEvent e) {
                        btnApplyActionPerformed(e);
                }

                private void btnApplyActionPerformed(ActionEvent evt) {
                        MainController mainController = MainController.getInstance();
                        JobseekerController jobseekerController = new JobseekerController();
                        if (LoginInfo.getLoginId() != null && LoginInfo.isJobseeker(LoginInfo.getLoginDomain())) {
                                try {
                                        Jobseeker jobseeker = jobseekerController.getJobseekerByEmail(LoginInfo.getLoginId());
                                        Response response = new Response("",
                                                textJobAdvertisementId.getText(),
                                                jobseeker.getID(),
                                                "D", String.valueOf(new Date())); 

                                        mainController.setResponse(response);
                                        mainController.gotoScreen(MainController.JASS_RESPONSE_CONFIRMATION, null);

                                } catch (Exception e) {
                                        logger.log(Level.SEVERE, null, e);
                                }
                        } else {
                                mainController.gotoScreen(MainController.JASS_LOGIN_FORM, JASSAdvertisementDetailForm.this);
                        }
                }
        }
}
